const express = require('express')
const router = express.Router()
const acticleAction = require('../Controller/acticle')
const categoryAction = require('../Controller/categorys')
const blogAction = require('../Controller/blog')



function getClientIp(req) {
  var ipAddress;
  var forwardedIpsStr = req.headers['X-Forwarded-For'];//判断是否有反向代理头信息
  if (forwardedIpsStr) {//如果有，则将头信息中第一个地址拿出，该地址就是真实的客户端IP；
      var forwardedIps = forwardedIpsStr.split(',');
      ipAddress = forwardedIps[0];
  }
  if (!ipAddress) {//如果没有直接获取IP；
      ipAddress = req.connection.remoteAddress;
  }
  return ipAddress;
};

//主页
router.get('/', function (req, res) {
  console.log(getClientIp(req))
  let cate,focusPic,goods,clickRank,newlist,piclist,focusPic1,goods1,homelist;
  categoryAction.all(function (doc) {
     cate = doc
     blogAction.focusPic(0,3).then(function(doc){
      focusPic= doc
      return blogAction.good(0,2) 
     }).then(function(doc){
      goods= doc
      return blogAction.clickRank()
     }).then(function(doc){
      clickRank= doc
      return blogAction.newUP()
     }).then(function(doc){
      newlist= doc
      return blogAction.pic()
     }).then(function(doc){
       piclist = doc
      return blogAction.focusPic(3,6)
     }).then(function(doc){
      focusPic1= doc
      return blogAction.good(2,5) 
     }).then(function(doc){
      goods1= doc
      return blogAction.homeList()
     }).then(function(doc){
       homelist= doc
     res.render('blog/index.html',{cate,focusPic,goods,clickRank,newlist,piclist,focusPic1,goods1,homelist})
     })


  })

 
})

//分类列表
router.get('/category:id',function (req, res) {
  let page = req.query.page
  let id = req.params.id
  if(!page){
    page =1
  }
  
  let cate,list,topRank,clickRank,piclist,newlist,tagAll;
  categoryAction.all(function (doc) {
     cate =doc;
     if(id==3){
      blogAction.time(id,page).then(function(doc){
        let timelist= doc.list
        res.render('blog/time.html',{cate,timelist})
        })
      return;
    }
     //点击排行
     blogAction.clickRank().then(function(doc){
      clickRank = doc
      //博文列表
      return blogAction.blogByCate(id,page)
    }).then(function(doc){
      list = doc;
      //推荐排行
      return  blogAction.topRank()
    }).then(function(doc){
      topRank = doc
      //图文
      return blogAction.pic()
    }).then(function(doc){
      piclist=doc;
      //最近更新
      return blogAction.newUP() 
    }).then(function(doc){
      newlist= doc
      return blogAction.tagAll()    
    }).then(function(doc){
      tagAll = doc
      res.render('blog/list.html',{cate,'list':list.list,topRank,clickRank,piclist,newlist,page,'count':list.count,tagAll})
    })

  })

})

//分类文章
router.get('/category:Cateid/:Artid',function (req, res) {
  let Artid = req.params.Artid
  let Cateid = req.params.Cateid
  let cate,clickRank,topRank,article,piclist,tagAll;

 categoryAction.all(function (doc) {
    cate =doc;
    //点击排行
    blogAction.clickRank().then(function(doc){
      clickRank = doc
    //博文
      return blogAction.blogById(Artid)
    }).then(function(doc){
      article=JSON.parse(JSON.stringify(doc));
      //推荐排行
      return  blogAction.topRank()
    }).then(function(doc){
      topRank=doc;
      //图文
      return blogAction.pic()
    }).then(function(doc){
      piclist =doc
      return blogAction.tagAll()  
    }).then(function(doc){
      tagAll = doc
      res.render('blog/arctic.html',{cate,clickRank,topRank,article,piclist,tagAll,Cateid})
    }) 
  })
  
  //更新浏览量
  blogAction.viewUP(Artid)
})

//点赞
router.post('/digg:id', function (req, res) {
  let id = req.params.id
  blogAction.digg(id).then(function(doc){
    res.json({code:1,num:doc.countInfo.likeCount})
  })
})

//获得点赞数
router.get('/digg:id', function (req, res) {
  let id = req.params.id
  blogAction.getdigg(id).then(function(doc){
    res.json({code:1,num:doc})
  })
})


//搜索
router.post('/search',function(req,res){
  let page = req.body.page
  let keyword = req.body.keyword
  if(!page){
    page =1
  }
  let cate,list,topRank,clickRank,piclist,newlist;
  categoryAction.all(function (doc) {
     cate =doc;
       //点击排行
     blogAction.clickRank().then(function(doc){
      clickRank = doc
      //博文列表
      return blogAction.search(keyword,page)
    }).then(function(doc){
      list = doc;
      //推荐排行
      return  blogAction.topRank()
    }).then(function(doc){
      topRank = doc
      //图文
      return blogAction.pic()
    }).then(function(doc){
      piclist=doc;
      //最近更新
      return blogAction.newUP() 
    }).then(function(doc){
      newlist= doc
      res.render('blog/search.html',{cate,'list':list.list,topRank,clickRank,piclist,newlist,page,'count':list.count,'q':keyword})
    })

  })


})

router.get('/search',function(req,res){
  let page = req.params.page
  let keyword = req.params.keyword
  if(!page){
    page =1
  }
  let cate,list,topRank,clickRank,piclist,newlist;
  categoryAction.all(function (doc) {
     cate =doc;
       //点击排行
     blogAction.clickRank().then(function(doc){
      clickRank = doc
      //博文列表
      return blogAction.search(keyword,page)
    }).then(function(doc){
      list = doc;
      //推荐排行
      return  blogAction.topRank()
    }).then(function(doc){
      topRank = doc
      //图文
      return blogAction.pic()
    }).then(function(doc){
      piclist=doc;
      //最近更新
      return blogAction.newUP() 
    }).then(function(doc){
      newlist= doc
      res.render('blog/search.html',{cate,'list':list.list,topRank,clickRank,piclist,newlist,page,'count':list.count,'q':keyword})
    })

  })


})

//tag列表
router.get('/taglist:id',function(req,res){
  let page = req.query.page
  let id = req.params.id
  if(!page){
    page =1
  }
  
  let cate,list,topRank,clickRank,piclist,newlist,tagAll;
  categoryAction.all(function (doc) {
     cate =doc;
     if(id==3){
      blogAction.time(id,page).then(function(doc){
        let timelist= doc.list
        res.render('blog/time.html',{cate,timelist})
        })
      return;
    }
     //点击排行
     blogAction.clickRank().then(function(doc){
      clickRank = doc
      //博文列表
      return blogAction.blogByTag(id,page)
    }).then(function(doc){
      list = doc;
      //推荐排行
      return  blogAction.topRank()
    }).then(function(doc){
      topRank = doc
      //图文
      return blogAction.pic()
    }).then(function(doc){
      piclist=doc;
      //最近更新
      return blogAction.newUP() 
    }).then(function(doc){
      newlist= doc
      return blogAction.tagAll()    
    }).then(function(doc){
      tagAll = doc
      res.render('blog/list.html',{cate,'list':list.list,topRank,clickRank,piclist,newlist,page,'count':list.count,tagAll})
    })

  })

  

})



module.exports = router